import {Divider, message, Tabs } from 'antd'
import React, { useState, useRef, useEffect } from 'react'
import { PageHeaderWrapper } from '@ant-design/pro-layout'
import ProTable, { ProColumns, ActionType } from '@ant-design/pro-table'
import { connect, history, Link } from 'umi'
import { queryRule, updateHireBpmInfo, sendEmpBPM } from '../service'
import {socialTraineeStateArray } from '@/utils/constant'
import CreateForm from './components/CreateForm'

const  { TabPane } = Tabs;

const TableList = () => {
  const actionRef = useRef()
  const actionRef2 = useRef()
  const [done, setDone] = useState(false)
  const [visible,setVisible] = useState(false)
  const [current, setCurrent] = useState(undefined)
  const [loading,setLoading] = useState(false);

  const itemEdit = (item) => {
    history.push(`/info/edit/${item.oid}`)
  }

  const itemDetail = (item) => {
    history.push(`/info/details/${item.oid}`)
  }

  const employ = (item) => {
    setCurrent(item)
  }

  const columns = [
    {
      title: '编号',
      dataIndex: 'employeeOid',
      hideInSearch: true,
    },
    {
      title: '姓名',
      dataIndex: 'name',
      render: (item, record) => (
        <Link to={`/info/details/${record.oid}`}>{item}</Link>
      ),
    },
    {
      title: '公司',
      dataIndex: 'orgname',
    },
    {
      title: '部门',
      dataIndex: 'unitname',
    },
    {
      title: '岗位',
      dataIndex: 'jobname',
    },
    {
      title: '职级',
      dataIndex: 'jobLevel',
      hideInSearch: true,
    },
    {
      title: '录用状态',
      dataIndex: 'status',
      hideInSearch: true,
      valueEnum: {
        0: { text: '待发起', status: 'Default' },
        1: { text: '填写完以保存', status: 'Default' },
        2: { text: '审批中', status: 'Processing' },
        3: { text: '已审批', status: 'Success' },
        4: { text: '流程作废', status: 'Default' },
        5: { text: '发送完毕', status: 'Default' },
        6: { text: '流程被驳回', status: 'Default' }
      },
    },
    {
      title: '招聘类型',
      dataIndex: 'socialTrainee',
      valueEnum: {
        0: { text: socialTraineeStateArray[0] },
        1: { text: socialTraineeStateArray[1] }
      },
    },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      render: (item, record) => (
        <>
          <a onClick={() => itemEdit(record)}>修改</a>
          <Divider type="vertical"/>
          <a onClick={() => itemDetail(record)}>详情</a>
          {
            (record.status === 0 || record.status === 1 || record.status === 6) && <>
              <Divider type="vertical"/>
              <a onClick={() => employ(record)}>发起录用</a>
            </>
          }
          {
            (record.checkUrl) && <>
              <Divider type="vertical"/>
              <a onClick={() => window.open(record.checkUrl)}>审批详情</a>
            </>
          }
        </>
      ),
    },
  ]

  const handleCancel = () => {
    setCurrent(null)
    setVisible(false)
    setDone(true)
  };

  const handleSubmit = (values) => {
    setLoading(true)
    updateHireBpmInfo(values).then(res=>{
      if(res.flag){
        if(values.type !== 'save'){
          sendEmpBPM(values.oid,values.isReinstate).then((res)=>{
            if (res.flag){
              window.open(res.data);
              message.success(`发起录用成功！`);
              handleCancel();
              actionRef.current.reload();
            } else {
              message.error(res.error);
            }
          })
        }else{
          message.success(`保存成功！`);
          handleCancel();
          actionRef.current.reload();
        }
      }else{
        message.error(res.error)
      }
      setLoading(false)
    })
  }

  const callback = (key) => {
    if(key === '0'){
      actionRef.current &&  actionRef.current.reload();
    }else{
      actionRef2.current && actionRef2.current.reload();
    }
  }

  return (
    <PageHeaderWrapper>
      <Tabs animated={false} defaultActiveKey="0" onChange={callback}>
        <TabPane tab="待办理" key="0">
          <ProTable
            actionRef={actionRef}
            rowKey="oid"
            loading={loading}
            request={(params) => queryRule({...params, status:[0,1,2,5]})}
            columns={columns}
          />
          <CreateForm
            values={{}}
            loading={loading}
            done={done}
            current={current}
            onCancel={handleCancel}
            onSubmit={handleSubmit}
          />
        </TabPane>
        <TabPane tab="已办理" key="1">
          <ProTable
            actionRef={actionRef2}
            rowKey="oid"
            request={(params) => queryRule({...params, status:[3,4]})}
            columns={columns}
          />
        </TabPane>
      </Tabs>
    </PageHeaderWrapper>
  )
}

export default connect(({  }) => ({
}))(TableList)
